package com.dianxin.smartpark.service.common.impl;

import java.util.Date;
import java.util.List;

import com.chinatelecom.response.common.CommonResponseBody;
import com.chinatelecom.response.utils.BeanUtil;
import com.chinatelecom.response.utils.ResponseBodyWrapperUtil;
import com.chinatelecom.response.constant.ErrMsgEnum;

import com.chinatelecom.response.utils.StringUtils;
import com.dianxin.smartpark.dao.dataobject.common.SmartParkMessage;
import com.dianxin.smartpark.dao.dataobject.common.SmartParkMessageExample;
import com.dianxin.smartpark.dao.mapper.common.SmartParkMessageMapper;
import com.dianxin.smartpark.dao.qo.common.SmartParkMessageQO;
import com.dianxin.smartpark.service.common.SmartParkMessageService;
import com.dianxin.smartpark.service.vo.common.SmartParkMessageVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * 消息管理业务层处理
 * 
 * @author chenjiasong
 * @date 2021-08-16
 */
@Service
public class SmartParkMessageServiceImpl implements SmartParkMessageService
{
    private final static Logger logger = LoggerFactory.getLogger(SmartParkMessageServiceImpl.class);
    @Resource
    private SmartParkMessageMapper smartParkMessageMapper;

    /**
     * 查询消息管理
     * 
     * @param qo 消息管理
     * @return 消息管理
     */
    @Override
    public CommonResponseBody<SmartParkMessageVO> selectSmartParkMessage(SmartParkMessageQO qo)
    {
        if(StringUtils.isNull(qo.getId())){
            return ResponseBodyWrapperUtil.fail(ErrMsgEnum.ERROR_PARAME);
        }
        SmartParkMessageExample smartParkMessageExample = new SmartParkMessageExample();
        SmartParkMessageExample.Criteria criteria = smartParkMessageExample.createCriteria();
        if (qo.getTenantId() != null) {
            criteria.andTenantIdEqualTo(qo.getTenantId());
        }
        criteria.andIsDeletedEqualTo(0);
        criteria.andIdEqualTo(qo.getId());
        List<SmartParkMessage> passRecords = smartParkMessageMapper.selectByExample(smartParkMessageExample);
        return ResponseBodyWrapperUtil.successWrapper(StringUtils.isNotEmpty(passRecords)?passRecords.get(0):null,SmartParkMessageVO.class);
    }

    /**
     * 查询消息管理列表
     * 
     * @param qo 消息管理
     * @return 消息管理
     */
    @Override
    public CommonResponseBody<List<SmartParkMessageVO>> selectSmartParkMessageList(SmartParkMessageQO qo)
    {
        SmartParkMessageExample smartParkMessageExample = new SmartParkMessageExample();
        SmartParkMessageExample.Criteria criteria = smartParkMessageExample.createCriteria();
        if (qo.getTenantId() != null) {
            criteria.andTenantIdEqualTo(qo.getTenantId());
        }
        if (qo.getBusiId() != null) {
            criteria.andBusiIdEqualTo(qo.getBusiId());
        }
        if (qo.getBusiType() != null) {
            criteria.andBusiTypeEqualTo(qo.getBusiType());
        }
        criteria.andIsDeletedEqualTo(0);
        if (qo.getBusiType() != null) {
            smartParkMessageExample.setUserId(qo.getUserId());
        }
        if (qo.getNeedPage()) {
            smartParkMessageExample.setOrderByClause("gmt_create desc limit " + (qo.getPageIndex() - 1) * qo.getPageSize() + "," + qo.getPageSize());
        } else {
            smartParkMessageExample.setOrderByClause("gmt_create desc");
        }
        List<SmartParkMessage> passRecords = smartParkMessageMapper.selectByExample(smartParkMessageExample);
        long count = smartParkMessageMapper.countByExample(smartParkMessageExample);
        return ResponseBodyWrapperUtil
                .successListWrapper(passRecords, count, qo, SmartParkMessageVO.class);
    }

    /**
     * 新增消息管理
     * 
     * @param vo 消息管理
     * @return 结果
     */
    @Override
    public CommonResponseBody<Boolean> insertSmartParkMessage(@Valid SmartParkMessageVO vo)
    {
        // 验证 todo
        SmartParkMessage smartParkMessage = new SmartParkMessage();
        try {
            BeanUtil.insertConversion(vo, smartParkMessage);
        } catch (Exception e) {
            logger.error("", e);
        }
        smartParkMessage.beforeInsert();
        return ResponseBodyWrapperUtil.toResData(smartParkMessageMapper.insertSelective(smartParkMessage));
    }

    /**
     * 修改消息管理
     * 
     * @param vo 消息管理
     * @return 结果
     */
    @Override
    public CommonResponseBody<Boolean> updateSmartParkMessage(@Valid SmartParkMessageVO vo)
    {
        // 验证 todo
        SmartParkMessage smartParkMessage = new SmartParkMessage();
        try {
            BeanUtil.insertConversion(vo, smartParkMessage);
        } catch (Exception e) {
            logger.error("", e);
        }
        smartParkMessage.beforeUpdate();
        return ResponseBodyWrapperUtil.toResData(smartParkMessageMapper.updateByPrimaryKeySelective(smartParkMessage));
    }

    /**
     * 删除消息管理信息
     * 
     * @param vo 消息管理
     * @return 结果08
     */
    @Override
    public CommonResponseBody<Boolean> deleteSmartParkMessage(@Valid SmartParkMessageVO vo)
    {
        // 验证 todo
        if(StringUtils.isNull(vo.getId())){
            return ResponseBodyWrapperUtil.fail(ErrMsgEnum.ERROR_PARAME);
        }
        SmartParkMessage smartParkMessage = new SmartParkMessage();
        try {
            BeanUtil.insertConversion(vo, smartParkMessage);
        } catch (Exception e) {
            logger.error("", e);
        }
        smartParkMessage.beforeDelete();
        return ResponseBodyWrapperUtil.toResData(smartParkMessageMapper.updateByPrimaryKeySelective(smartParkMessage));
    }
}
